//
//  ViewController.m
//  03-UIGestureRecognizer
//
//  Created by mafeng on 16/7/18.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 手势识别器
    // UIGestureRecognizer 作为所有手势的基类
    // 分为7种
    // 1.轻拍
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 点击几次触发, 默认为1
    tap.numberOfTapsRequired = 2;
    // 几个手指触发(按option - alt), 默认为1
    tap.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:tap];
    // 2.长按 longPress
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 长按几秒后开始, 默认0.5秒
    longPress.minimumPressDuration = 2;
    // 默认10像素, 长按失败之前, 最大的移动距离
    longPress.allowableMovement = 10;
    [self.view addGestureRecognizer:longPress];
    // 3.轻扫
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
    // direct 可以设置多个, 前提是多个方向触发的行为要相同
    // 如果多个方法触发的行为不同, 创建多个轻扫的手势
    // direction 是按位枚举
    // 0 0 1 1 , 如果4个全选, 是1 1 1 1
    // 按位枚举, 4个选项 -> 1 1 1 1
    // 全选的情况 1(4) 1(3) 1(2) 1(1) 1(0)
    // 左右 与 上下有冲突
    swip.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
   // [self.view addGestureRecognizer:swip];
    // 练习: imageView添加轻扫换图
    // 涉及到响应到响应者链, 向右的时候图片响应, 向左的时候view响应
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 414, 736)];
    NSLog(@"%@", self.view);
    imageView.image = [UIImage imageNamed:@"123"];
    imageView.userInteractionEnabled = YES;
    [self.view addSubview:imageView];
    
    UISwipeGestureRecognizer *changeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeRight:)];
    UISwipeGestureRecognizer *changeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeLeft:)];
    changeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    //[imageView addGestureRecognizer:changeRight];
    // [imageView addGestureRecognizer:changeLeft];
    // 4.平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    // [imageView addGestureRecognizer:pan];
    // 5. 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imageView addGestureRecognizer:pinch];
    // 6. 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [imageView addGestureRecognizer:rotation];
    // 7. 屏幕边缘手势
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];
    screenEdgePan.edges = UIRectEdgeRight;
    [imageView addGestureRecognizer:screenEdgePan];
}

- (void)changeRight:(UISwipeGestureRecognizer *)swip {
    NSLog(@"改变图片");
    UIImageView *view = (UIImageView *)swip.view;
    view.image = [UIImage imageNamed:@"456"];
}
- (void)changeLeft:(UISwipeGestureRecognizer *)swip {
    NSLog(@"改变图片");
    UIImageView *view = (UIImageView *)swip.view;
    view.image = [UIImage imageNamed:@"123"];
}
// ⬇️ 将两种方法变为一种不可以实现

- (void)change:(UISwipeGestureRecognizer *)swip {
    // 下面两个同时执行
    if (swip.direction & UISwipeGestureRecognizerDirectionLeft) {
         UIImageView *view = (UIImageView *)swip.view;
        view.image = [UIImage imageNamed:@"123"];
    }
    if (swip.direction & UISwipeGestureRecognizerDirectionRight) {
        UIImageView *view = (UIImageView *)swip.view;
        view.image = [UIImage imageNamed:@"456"];
    }
}
// 轻拍
- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}
// 轻扫
- (void)swipAction:(UIGestureRecognizer *)swip {
    NSLog(@"轻扫");
}
// 长按
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
    NSLog(@"长按");
}
// 平移
- (void)panAction:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateBegan) {
        // 平移开始
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        NSLog(@"pan");
        // 平移手势, 根据其view上移动的过程, 得到最终点
        CGPoint point = [pan translationInView:pan.view];
        NSLog(@"%f, %f", point.x, point.y);
        // 根据Point使pan.view移动
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform,point.x, point.y);
        // pan移动的点置空
        [pan setTranslation:CGPointZero inView:self.view];
    } else if (pan.state == UIGestureRecognizerStateEnded) {
        // 平移结束
        pan.view.transform = CGAffineTransformIdentity;
    }
   
}
// 捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
    NSLog(@"%f", pinch.scale);
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1.0f;
}
// 旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {
    // 转化中根据上次转换继续改变
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    // 旋转的弧度置为0
    rotation.rotation = 0;
}
//
- (void)screenAction:(UIScreenEdgePanGestureRecognizer *)screen {
    NSLog(@"屏幕边缘手势");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
